//配置数据库
const mysql      = require('mysql');
//抛出匿名函数
module.exports=(sql,params)=>{
    //创建配置对象
    const connection = mysql.createConnection({
        host     : 'localhost',
        port     : 3306,
        user     : 'root',
        password : 'root',
        database : '1710a'
    });
    //连接数据库
    connection.connect(err=>{
        if(err) throw err;
        console.log('mysql is connected....')
    });
    
    //有回调需用promise
    return new Promise((resolve, reject) => {
        connection.query(sql,params, function (error, results, fields) {
            if(error){
                reject(error)
            }else{
                resolve(results)
            }
        });
        //关闭连接
        connection.end();
    })
    
}